home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / prsr_lib / bsp_wrt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-29  |  11.6 KB  |  297 lines

  1. /******************************************************************************
  2. * Bsp_Wrt.c - Bspline handling routines - write to file.              *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Aug. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "prsr_loc.h"
  11.  
  12. /*****************************************************************************
  13. * DESCRIPTION:                                                               M
  14. * Writes Bspline curve(s) list into file. Returns TRUE if succesful, FALSE   M
  15. * otherwise.                                     M
  16. *   If Comment is NULL, no comment is wrriten, if "" only internal comment   M
  17. * is written.                                     M
  18. *                                                                            *
  19. * PARAMETERS:                                                                M
  20. *   Crvs:       To write to file FileName.                                   M
  21. *   FileName:   Name of file to open so we can write Crvs in.                M
  22. *   Indent:     Primary indentation. All information will be written         M
  23. *               from the column specified by Indent.                         M
  24. *   Comment:    Optional, to describe the geometry.                          M
  25. *   ErrStr:     If an error occurs, to describe the error.                   M
  26. *                                                                            *
  27. * RETURN VALUE:                                                              M
  28. *   int:        TRUE if succesful, FALSE otherwise.                          M
  29. *                                                                            *
  30. * KEYWORDS:                                                                  M
  31. *   BspCrvWriteToFile, files, write                                          M
  32. *****************************************************************************/
  33. int BspCrvWriteToFile(CagdCrvStruct *Crvs,
  34.               char *FileName,
  35.               int Indent,
  36.               char *Comment,
  37.               char **ErrStr)
  38. {
  39.     int i, Handler;
  40.     FILE *f;
  41.  
  42.     if ((f = fopen(FileName, "w")) == NULL) {
  43.     *ErrStr = "Fail to open file";
  44.     return FALSE;
  45.     }
  46.     Handler = IritPrsrOpenStreamFromFile(f, FALSE,
  47.                      IritPrsrSenseBinaryFile(FileName),
  48.                      FALSE);
  49.  
  50.     i = BspCrvWriteToFile2(Crvs, Handler, Indent, Comment, ErrStr);
  51.  
  52.     IritPrsrCloseStream(Handler, TRUE);
  53.  
  54.     return i;
  55. }
  56.  
  57. /*****************************************************************************
  58. * DESCRIPTION:                                                               M
  59. * Writes Bspline curve(s) list into file. Returns TRUE if succesful, FALSE   M
  60. * otherwise. The file descriptor is not closed.                     M
  61. *   If Comment is NULL, no comment is wrriten, if "" only internal comment   M
  62. * is written.                                     M
  63. *                                                                            *
  64. * PARAMETERS:                                                                M
  65. *   Crvs:       To write to open stream.                                 M
  66. *   Handler:    A handler to the open stream.                     M
  67. *   Indent:     Primary indentation. All information will be written         M
  68. *               from the column specified by Indent.                         M
  69. *   Comment:    Optional, to describe the geometry.                          M
  70. *   ErrStr:     If an error occurs, to describe the error.                   M
  71. *                                                                            *
  72. * RETURN VALUE:                                                              M
  73. *   int:        TRUE if succesful, FALSE otherwise.                          M
  74. *                                                                            *
  75. * KEYWORDS:                                                                  M
  76. *   BspCrvWriteToFile2, files, write                                         M
  77. *****************************************************************************/
  78. int BspCrvWriteToFile2(CagdCrvStruct *Crvs,
  79.                int Handler,
  80.                int Indent,
  81.                char *Comment,
  82.                char **ErrStr)
  83. {
  84.     int i, j, KVLen, MaxCoord;
  85.  
  86.     if (Comment != NULL) {
  87.     _IPFprintf(Handler, Indent, "#\n");
  88.     _IPFprintf(Handler, Indent, "# cagd_lib - bspline curve(s) dump.\n");
  89.     _IPFprintf(Handler, Indent, "#\n");
  90.     _IPFprintf(Handler, Indent, "# %s\n", Comment);
  91.     _IPFprintf(Handler, Indent, "#\n");
  92.     }
  93.  
  94.     *ErrStr = NULL;
  95.  
  96.     while (Crvs) {
  97.     MaxCoord = CAGD_NUM_OF_PT_COORD(Crvs -> PType);
  98.  
  99.     if (Crvs -> GType != CAGD_CBSPLINE_TYPE) {
  100.         *ErrStr = "Given curve(s) is (are) not Bspline curve(s)";
  101.         break;
  102.     }
  103.     _IPFprintf(Handler, Indent, "[CURVE BSPLINE %d %d %c%c\n",
  104.         Crvs -> Length, Crvs -> Order,
  105.         CAGD_IS_RATIONAL_PT(Crvs -> PType) ? 'P' : 'E',
  106.         MaxCoord + '0');
  107.     Indent += 4;
  108.  
  109.     /* Put out the knot vectors: */
  110.     _IPFprintf(Handler, Indent, Crvs -> Periodic ? "[KVP" : "[KV");
  111.     KVLen = Crvs -> Order + Crvs -> Length +
  112.                 (Crvs -> Periodic ? Crvs -> Order - 1 : 0);
  113.     for (i = 0; i < KVLen; i++) {
  114.         if (i && i % MAX_KNOTS_PER_LINE == 0) {
  115.         _IPFprintf(Handler, 0, "\n");
  116.         _IPFprintf(Handler, Indent + 4, "");
  117.         }
  118.         _IPFprintf(Handler, 0, " %s", _IPReal2Str(Crvs -> KnotVector[i]));
  119.     }
  120.     _IPFprintf(Handler, 0, "]\n");
  121.  
  122.     /* Put out the control polygon. */
  123.     for (i = 0; i < Crvs -> Length; i++) {
  124.         _IPFprintf(Handler, Indent, "[");
  125.         if (CAGD_IS_RATIONAL_PT(Crvs -> PType))
  126.         _IPFprintf(Handler, 0, "%s ",
  127.                _IPReal2Str(Crvs -> Points[0][i]));
  128.         for (j = 1; j <= MaxCoord; j++) {
  129.         _IPFprintf(Handler, 0, "%s",
  130.                _IPReal2Str(Crvs -> Points[j][i]));
  131.         if (j < MaxCoord)
  132.             _IPFprintf(Handler, 0, " ");
  133.         }
  134.         _IPFprintf(Handler, 0, "]\n");
  135.     }
  136.  
  137.     Indent -= 4;
  138.     _IPFprintf(Handler, Indent, "]\n");
  139.  
  140.     Crvs = Crvs -> Pnext;
  141.     }
  142.  
  143.     return *ErrStr == NULL;
  144. }
  145.  
  146. /*****************************************************************************
  147. * DESCRIPTION:                                                               M
  148. * Writes Bspline surface(s) list into file. Returns TRUE if succesful, FALSE M
  149. * otherwise.                                     M
  150. *   If Comment is NULL, no comment is wrriten, if "" only internal comment   M
  151. * is written.                                     M
  152. *                                                                            *
  153. * PARAMETERS:                                                                M
  154. *   Srfs:       To write to file FileName.                                   M
  155. *   FileName:   Name of file to open so we can write Srfs in.                M
  156. *   Indent:     Primary indentation. All information will be written         M
  157. *               from the column specified by Indent.                         M
  158. *   Comment:    Optional, to describe the geometry.                          M
  159. *   ErrStr:     If an error occurs, to describe the error.                   M
  160. *                                                                            *
  161. * RETURN VALUE:                                                              M
  162. *   int:        TRUE if succesful, FALSE otherwise.                          M
  163. *                                                                            *
  164. * KEYWORDS:                                                                  M
  165. *   BspSrfWriteToFile, files, write                                          M
  166. *****************************************************************************/
  167. int BspSrfWriteToFile(CagdSrfStruct *Srfs,
  168.               char *FileName,
  169.               int Indent,
  170.               char *Comment,
  171.               char **ErrStr)
  172. {
  173.     int i, Handler;
  174.     FILE *f;
  175.  
  176.     if ((f = fopen(FileName, "w")) == NULL) {
  177.     *ErrStr = "Fail to open file";
  178.     return FALSE;
  179.     }
  180.     Handler = IritPrsrOpenStreamFromFile(f, FALSE,
  181.                      IritPrsrSenseBinaryFile(FileName),
  182.                      FALSE);
  183.  
  184.     i = BspSrfWriteToFile2(Srfs, Handler, Indent, Comment, ErrStr);
  185.  
  186.     IritPrsrCloseStream(Handler, TRUE);
  187.  
  188.     return i;
  189. }
  190.  
  191. /*****************************************************************************
  192. * DESCRIPTION:                                                               M
  193. * Writes Bspline surface(s) list into file. Returns TRUE if succesful, FALSE M
  194. * otherwise. The file descriptor is not closed.                     M
  195. *   If Comment is NULL, no comment is wrriten, if "" only internal comment   M
  196. * is written.                                     M
  197. *                                                                            *
  198. * PARAMETERS:                                                                M
  199. *   Srfs:       To write to open stream.                                     M
  200. *   Handler:    A handler to the open stream.                     M
  201. *   Indent:     Primary indentation. All information will be written         M
  202. *               from the column specified by Indent.                         M
  203. *   Comment:    Optional, to describe the geometry.                          M
  204. *   ErrStr:     If an error occurs, to describe the error.                   M
  205. *                                                                            *
  206. * RETURN VALUE:                                                              M
  207. *   int:        TRUE if succesful, FALSE otherwise.                          M
  208. *                                                                            *
  209. * KEYWORDS:                                                                  M
  210. *   BspSrfWriteToFile2, files, write                                         M
  211. *****************************************************************************/
  212. int BspSrfWriteToFile2(CagdSrfStruct *Srfs,
  213.                int Handler,
  214.                int Indent,
  215.                char *Comment,
  216.                char **ErrStr)
  217. {
  218.     int i, j, KVLen, MaxCoord;
  219.     CagdRType *KnotVector;
  220.  
  221.     if (Comment != NULL) {
  222.     _IPFprintf(Handler, Indent, "#\n");
  223.     _IPFprintf(Handler, Indent, "# cagd_lib - bspline Srf(s) dump.\n");
  224.     _IPFprintf(Handler, Indent, "#\n");
  225.     _IPFprintf(Handler, Indent, "# %s\n", Comment);
  226.     _IPFprintf(Handler, Indent, "#\n");
  227.     }
  228.  
  229.     *ErrStr = NULL;
  230.  
  231.     while (Srfs) {
  232.     MaxCoord = CAGD_NUM_OF_PT_COORD(Srfs -> PType);
  233.  
  234.     if (Srfs -> GType != CAGD_SBSPLINE_TYPE) {
  235.         *ErrStr = "Given surface(s) is (are) not bspline surface(s)";
  236.         break;
  237.     }
  238.     _IPFprintf(Handler, Indent, "[SURFACE BSPLINE %d %d %d %d %c%c\n",
  239.         Srfs -> ULength, Srfs -> VLength,
  240.         Srfs -> UOrder, Srfs -> VOrder,
  241.         CAGD_IS_RATIONAL_PT(Srfs -> PType) ? 'P' : 'E',
  242.         MaxCoord + '0');
  243.     Indent += 4;
  244.  
  245.     /* Put out the knot vectors: */
  246.     for (i = 0; i < 2; i++) {
  247.         if (i == 0) {
  248.         KnotVector = Srfs -> UKnotVector;
  249.         KVLen = Srfs -> ULength + Srfs -> UOrder +
  250.                 (Srfs -> UPeriodic ? Srfs -> UOrder - 1 : 0);
  251.         _IPFprintf(Handler, Indent,
  252.                Srfs -> UPeriodic ? "[KVP" : "[KV");
  253.         }
  254.         else {
  255.         KnotVector = Srfs -> VKnotVector;
  256.         KVLen = Srfs -> VLength + Srfs -> VOrder +
  257.                 (Srfs -> VPeriodic ? Srfs -> VOrder - 1 : 0);
  258.         _IPFprintf(Handler, Indent,
  259.                Srfs -> VPeriodic ? "[KVP" : "[KV");
  260.         }
  261.  
  262.         for (j = 0; j < KVLen; j++) {
  263.         if (j && j % MAX_KNOTS_PER_LINE == 0) {
  264.             _IPFprintf(Handler, 0, "\n");
  265.             _IPFprintf(Handler, Indent + 4, "");
  266.         }
  267.         _IPFprintf(Handler, 0, " %s", _IPReal2Str(KnotVector[j]));
  268.         }
  269.         _IPFprintf(Handler, 0, "]\n");
  270.     }
  271.  
  272.     /* Put out the control mesh. */
  273.     for (i = 0; i < Srfs -> VLength * Srfs -> ULength; i++) {
  274.         if (i && i % Srfs -> ULength == 0)
  275.         _IPFprintf(Handler, 0, "\n");/* Put empty lines between raws.*/
  276.  
  277.         _IPFprintf(Handler, Indent, "[");
  278.         if (CAGD_IS_RATIONAL_PT(Srfs -> PType))
  279.         _IPFprintf(Handler, 0, "%s ",
  280.                _IPReal2Str(Srfs -> Points[0][i]));
  281.         for (j = 1; j <= MaxCoord; j++) {
  282.         _IPFprintf(Handler, 0, "%s",
  283.                _IPReal2Str(Srfs -> Points[j][i]));
  284.         if (j < MaxCoord) _IPFprintf(Handler, 0, " ");
  285.         }
  286.         _IPFprintf(Handler, 0, "]\n");
  287.     }
  288.  
  289.     Indent -= 4;
  290.     _IPFprintf(Handler, Indent, "]\n");
  291.  
  292.     Srfs = Srfs -> Pnext;
  293.     }
  294.  
  295.     return *ErrStr == NULL;
  296. }
  297.